package com.example.shoppingdemo.goods;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.goods.GoodsJieXi.Goodsjiexi;
import com.example.shoppingdemo.goods.model.GoodsModel;
import com.example.shoppingdemo.goods.model.SkuModel;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;

public class goodsActivity extends AppCompatActivity implements OnBannerListener {
    private TextView findmessage,Addbuycar;
    List<String> imglist;
    private RelativeLayout linearLayout;
    Banner banner;
    Goodsjiexi goodsjiexi;
    TextView name,pinpai,yuan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        goodsjiexi = new Goodsjiexi();
        linearLayout = findViewById(R.id.goods_classify);
        findmessage = findViewById(R.id.find_all_msg);
        Addbuycar = findViewById(R.id.add_car);
        imiView();
        requestData();
    }

    private void imiView(){
        findmessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(goodsActivity.this,mesgeActivity.class);
                startActivity(intent);
            }
        });

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(goodsActivity.this,Goods_classify.class);
                startActivity(intent);
            }
        });

        Addbuycar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(goodsActivity.this,BuycarActivity.class);
                startActivity(intent);
            }
        });

    }

    private void requestData(){
        name = findViewById(R.id.goods_name);
        banner = findViewById(R.id.banner);
        imglist = new ArrayList<>();
        pinpai = findViewById(R.id.goods_pai);
        yuan = findViewById(R.id.goods_money);

        goodsjiexi.FindGoodsResquest(27, new Goodsjiexi.ongoodslisetener() {
            @Override
            public void ongoodsmsg(List<GoodsModel> list, SkuModel skuModel) {
                String na = list.get(0).getName();
                String img = list.get(0).getAlbumPics();
                String buyname = list.get(0).getBrandName();
                String goodsyuan = String.valueOf(list.get(0).getPrice());

                name.setText(na);
                pinpai.setText(buyname);
                yuan.setText(goodsyuan);
                try {
                    StringBufferInputStream im = new StringBufferInputStream(img);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(im));
                    String temp = reader.readLine();
                    String[] result = temp.split(",");
                    for (int i = 0; i < result.length; i++) {
                        imglist.add(result[i]);
                    }
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        });



        banner.setAdapter(new BannerImageAdapter<String>(imglist) {

            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.itemView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        });
        //设置指示点
        banner.setIndicator(new CircleIndicator(this)).setLoopTime(3000).setOnBannerListener(this);
    }
    //设置圆形指示点，设置循环时间，设置监听器
    //监听每一个图片的点击事件
    @Override
    public void OnBannerClick(Object data, int position) {
        Log.i("tag", "你点了第"+position+"张轮播图");
    }

}